What is @turf/center?
@turf/center is a module from the Turf.js library that calculates the center of a given GeoJSON feature or set of features. It is useful for geographic data analysis and visualization, allowing developers to easily find the central point of various geometries such as polygons, lines, and multi-point collections.
What are @turf/center's main functionalities?
Calculate Center of a Polygon
This feature calculates the center point of a polygon. The code sample demonstrates how to create a polygon and then find its center using the @turf/center package.
const turf = require('@turf/center');
const polygon = turf.polygon([[
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41],
[-81, 41]
]]);
const center = turf(polygon);
console.log(center);
Calculate Center of a MultiPoint Collection
This feature calculates the center point of a collection of points (MultiPoint). The code sample shows how to create a MultiPoint collection and find its center using the @turf/center package.
const turf = require('@turf/center');
const multiPoint = turf.multiPoint([
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41]
]);
const center = turf(multiPoint);
console.log(center);
Calculate Center of a LineString
This feature calculates the center point of a LineString. The code sample demonstrates how to create a LineString and find its center using the @turf/center package.
const turf = require('@turf/center');
const lineString = turf.lineString([
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41]
]);
const center = turf(lineString);
console.log(center);
Other packages similar to @turf/center
geolib
Geolib is a library for geospatial calculations. It provides functions to calculate distances, areas, and center points of various geometries. Compared to @turf/center, geolib offers a broader range of geospatial utilities but may not be as specialized in handling GeoJSON data.
leaflet
Leaflet is a popular open-source JavaScript library for interactive maps. It includes functionalities to calculate the center of a map view or a set of coordinates. While Leaflet is primarily focused on map rendering and interaction, it can also be used for basic geospatial calculations similar to @turf/center.
geodesy
Geodesy is a library for geodesic calculations, including finding the center of a set of points. It is more focused on precise geodesic computations and may offer more accuracy for certain types of geospatial data compared to @turf/center.
@turf/center
center
Takes a Feature or FeatureCollection and returns the absolute center point of all features.
Parameters
Examples
var features = turf.points([
[-97.522259, 35.4691],
[-97.502754, 35.463455],
[-97.508269, 35.463245]
]);
var center = turf.center(features);
var addToMap = [features, center]
center.properties['marker-size'] = 'large';
center.properties['marker-color'] = '#000';
Returns Feature<Point> a Point feature at the absolute center point of all input features
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Installation
Install this single module individually:
$ npm install @turf/center
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf